
package action;

import beans.UserBean;
import db.Forum;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;

/**
 * Action for add new message.
 * @author Tomas CASTA
 */
@UrlBinding("/messageAdd.action")
public class AddMessageActionBean implements ActionBean {
    /** session */
    private HttpSession session;
    /** action context */
    private ActionBeanContext context;
    /** message id */
    private String id;
    /** text of new mesage */
    @Validate(required = true)
    private String msg;
    /** if user is logged  */
    private boolean logged;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public boolean isLogged() {
        return logged;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * Add new message.
     * @return Resolution
     */
    @DefaultHandler
    @DontValidate
    public Resolution add() {
        logged = false;
        
        /* User is logged */
        if (null != context.getRequest().getSession().getAttribute("user")) {
            logged = true;
            Forum forum = new Forum();
            
            /* Get user info from session */
            UserBean user = (UserBean) context.getRequest().getSession().getAttribute("user");
            forum.setDb(context.getServletContext());

            /* Get id from URI */
            try {
                int idValue = Integer.valueOf(this.id);

                /* Text is not empty */
                if (msg != null) {
                    
                    /* Add message to forum */
                    if (forum.addMessageToForum(idValue, msg, user)) {
                        return new RedirectResolution("/forum.action?id=" + id);
                    }
                    /* Error during adding, show index */
                    else {
                        return new ForwardResolution("/index.action");
                    }
                } /* Text is empty */
                else {                 

                    /* Set new error */
                    ValidationErrors errors = new ValidationErrors();
                    errors.add("msg", new LocalizableError("msg", "Musíte zadat text vzkazu!"));
                    getContext().setValidationErrors(errors);
                    return new ForwardResolution("/forum.action?id=" + id);
                }
            } catch (NumberFormatException e) {
                Logger.getLogger(DeleteMessageActionBean.class.getName()).log(Level.SEVERE, null, e);
                return new ForwardResolution("/index.action");
            }

        }
        /* User is not logged, show index */
        return new ForwardResolution("/index.action");
    }
}
